$(function(){
	window.sr = ScrollReveal();
	$("#backg").css("height",$(window).height());
	$(".loginpage").css("height",$(window).height());
	$(".cover").css("height",$(window).height());
	$("#zzdd").on("click",function(){
		$(".htmleaf-container").hide();
		$(".zzdd").fadeIn();
		$('.qrtab').addClass("ddtabon").siblings().removeClass("ddtabon");
		sr.reveal('.ddtab',{ 
			origin: 'bottom',
			scale: 1,
			distance: '0px',
			opacity: 0,
			reset:true,
			duration: 1000
		},100);
		$(".qrpage").show().siblings('.ddtabcon').hide();
		document.getElementById('qrcode').focus();
	})
	$("#czcx").on("click",function(){
		$(".htmleaf-container").hide();
		$(".czcx").fadeIn();
		document.getElementById('idcarczcx').focus();
	})
	$("#wzcx").on("click",function(){
		$(".htmleaf-container").hide();
		$(".wzcx").fadeIn();
		document.getElementById('idcarwzcx').focus();
	})
	$(".homebtn").click(function(){
		if ($('.askhelp').hasClass('videon')) {
			blocklink($('.askhelp'));
		} else{
			$(".htmleaf-container").fadeIn().siblings().hide();
			$(".czcx .idcheck").show().siblings().hide();
			$(".wzcx .idcheck").show().siblings().hide();
			$('.printbtn').addClass('noprintbtn');
			$("input").val("");
		};
	})
	$(".reload").click(function(){
		location.reload();
	})
	$(".ddtab").on("click",function(){
		$(this).addClass("ddtabon").siblings().removeClass("ddtabon");
		$('.printbtn').addClass('noprintbtn');
		$("input").val("");
		var num=$(this).index();
		$(".ddtabcons>div").eq(num).fadeIn().siblings('.ddtabcon').hide();
		if (num==0) {
			document.getElementById('qrcode').focus();
		};
		if (num==1) {
			document.getElementById('idcar').focus();
		}
	})
//	身份证验证
	$(".idcarcheck").bind('input propertychange',function(){
		var idlen=$(this).val().length;
		if (idlen==18) {
			$(".cover").show();
//			后台返回
			if ($(this).val()=="222222222222222222") {
				$(".cover").hide();
				checksuces($(this));
			} else{
				$(".cover").hide();
				swal({
					title: "WrongID",
					type: 'error',
					background: '#fff',
					allowOutsideClick: false,
					showConfirmButton: false,
					timer: 1200
				}).then(
					function () {},
					function(dismiss){
						if (dismiss === 'timer') {
				
						}
				});
			};
		} else{
			$('.printbtn').addClass('noprintbtn').removeClass('pulse');
		};
	})
//	条码验证
	$(".qrcode").on("change",function(){
		var qrval=$(this).val();
		var $this=$(this);
		$(".cover").show();
		$(this).blur();
		if (qrval=="JB17041400482") {
			$(".cover").hide();
			checksuces($(this));
		} else{
			$(".cover").hide();
			swal({
				title: "WrongQR",
				type: 'error',
				background: '#fff',
				showConfirmButton: false,
				timer: 1200
			}).then(
				function () {},
				function(dismiss){
					if (dismiss === 'timer') {
						$('.printbtn').addClass('noprintbtn').removeClass('pulse');
						$this.val("");
						$this.focus();
					}
			});
		};
		
	})
	$('.printbtn').on('click',function(){
		if ($(this).hasClass('noprintbtn')) {
			
		} else{
			swal({
				title: "打印中",
				type: 'warning',
				background: '#fff',
				showConfirmButton: false,
				timer: 3000
			}).then(
				function () {},
				function(dismiss){
					var num=$(".ddtabon").index();
					if (num==0) {
						document.getElementById('qrcode').focus().val;
					};
					if (num==1) {
						document.getElementById('idcar').focus();
					}
			});
		}
	})
	$('.printbtn2').on('click',function(){

			swal({
				title: "打印中",
				type: 'warning',
				background: '#fff',
				showConfirmButton: false,
				timer: 3000
			}).then(
				function () {},
				function(dismiss){
					
			});
	})
	$(".askhelp").click(function(){
		blocklink($(this));
	})
	var imgL=$(".pic .homebutton").size();
	var deg=360/imgL;
	var roY=0,roX=-10;
	var xN=0,yN=0;
	var play=null;

	$(".pic .homebutton").each(function(i){
		$(this).css({
//			<!--translateZ 定义2d旋转沿着z轴-->
			"transform":"rotateY("+i*deg+"deg) translateZ(300px)"
		});
//			<!--防止图片被拖拽-->
			$(this).attr('ondragstart','return false');
	});

	$(document).on('mousedown touchstart',function(ev){
		if (ev.type == "touchstart") {
			var x_=ev.originalEvent.touches[0].clientX;
			var y_=ev.originalEvent.touches[0].clientY;
		} else{
			var x_=ev.clientX;
			var y_=ev.clientY;
		};
//		var x_=ev.clientX;
//		var y_=ev.clientY;
//		var tx_=ev.originalEvent.touches[0].clientX;
//		var ty_=ev.originalEvent.touches[0].clientY;
		clearInterval(play);
			$(this).bind('mousemove touchmove',function(ev){
				if (ev.type == "touchmove") {
					var x=ev.originalEvent.touches[0].clientX;
					var y=ev.originalEvent.touches[0].clientY;
				} else{
					/*获取当前鼠标的坐标*/
					var x=ev.clientX;
					var y=ev.clientY;
				};		

		            /*两次坐标之间的距离*/
		            xN=x-x_;
//					yN=y-y_;
//				console.log("xN:"+xN);
//				if (isNaN(xN)) {
//					console.log('x:'+x);
//					console.log('x_:'+x_);
//				};
				roY+=xN*0.2;
				roX-=yN*0.1;
				var absroY = Math.abs(roY);
//				$('body').append('<div style="width:5px;height:5px;position:absolute;top:'+y+'px;left:'+x+'px;background-color:red"></div>');
				$('.pic').css({
	                 transform:'perspective(800px) rotateX('+roX+'deg) rotateY('+roY+'deg)'
				});
				
					/*之前的鼠标坐标*/
				if (ev.type == "touchmove") {
					x_=ev.originalEvent.touches[0].clientX;
					y_=ev.originalEvent.touches[0].clientY;
				} else{
			        x_=ev.clientX;
			        y_=ev.clientY;				
				};
			});
	}).on('mouseup  touchend',function(){
        $(this).unbind('mousemove touchmove');
        var play=setInterval(function(){
	        xN*=0.95;
	        yN*=0.95
	        if(Math.abs(xN)<1 && Math.abs(yN)<1){
	            clearInterval(play);
	        }
	        roY+=xN*0.2;
			roX-=yN*0.1;
			var absroY = Math.abs(roY);
			$('.pic').css({
	             transform:'perspective(800px) rotateX('+roX+'deg) rotateY('+roY+'deg)'
			});
        },30);
	});
});
function checksuces(input){
//	自助打单 验证成功
	if (input.attr("id")=="idcar" || input.attr("id")=="qrcode") {
		console.log("suces");
		swal({
			title: "成功",
			type: 'success',
			background: '#fff',
			allowOutsideClick: false,
			showConfirmButton: false,
			timer: 1200
		}).then(
			function () {},
			function(dismiss){
			if (dismiss === 'timer') {
				$('.printbtn').removeClass('noprintbtn').addClass("animated2 pulse");
				setTimeout(function() {
					$('.printbtn').removeClass('pulse');
				}, 2400);
				if (input.attr("id")=="qrcode") {
					input.val("");
					input.focus();
				}
			}
		});
	}
//	产能查询&违章查询 验证成功
	if (input.attr("id")=="idcarczcx" || input.attr("id")=="idcarwzcx") {
		swal({
			title: "成功",
			type: 'success',
			background: '#fff',
			allowOutsideClick: false,
			showConfirmButton: false,
			timer: 1200
		}).then(
			function () {},
			function(dismiss){
			if (dismiss === 'timer') {
				input.parents(".idcheck").hide().next(".biglist").fadeIn();
			}
		});
	}
};
//视频链接btn
function blocklink(blclbtn){
	$('.doCalling').append('<div class="callingMes" style="color:#C53929;width:100px;height:100px;text-align:center;position:absolute;left:0; right:0; top:0; bottom:0;margin:auto;">'+
								'正在呼叫<div class="spinner"><div class="double-bounce1"></div> <div class="double-bounce2"></div></div>'+
							'</div>');
	window.closeCalling=blclbtn;
	if (blclbtn.hasClass("videon")) {
		swal({
			width:'500px',
			imageHeight:'10px',
			title: "断开连接？",
			type: 'warning',
			background: 'rgba(255,255,255,1)',
			showConfirmButton: true,
			showCancelButton: true,
			confirmButtonText: "是",
			cancelButtonText : "否",
		}).then(
			function () {
				closeCalling.html('<img src="img/service.png"/>在线咨询');
				closeCalling.removeClass("videon");
				$(".vidiochat").fadeOut();
				rtcCall.endCall();
			},
			function(dismiss){}
		);
		
	} else{
		blclbtn.html('<img src="img/blocklink.png"/>断开链接');
		blclbtn.addClass("videon");
		$(".vidiochat").show().addClass("animated slideInLeft");
		setTimeout(function() {$('.vidiochat').removeClass('slideInLeft')}, 1000);
		getOneServerForChat();
	};
	
};


//登入页面
$(function(){
	$(".loginbtn").click(function(){
		//登入按钮
		if ($('#loginact').val()=="" || $('#loginpsw').val()=="") {
			swal({
				title: "请输入账号和密码",
				text: "",
				type: 'warning',
				background: '#fff',
				allowOutsideClick: false,
				showConfirmButton: false,
				timer: 1200,
//			}).then(
//				function () {
//					
//				},
//				function(dismiss){
//					if (dismiss === 'timer') {
//					
//					}
			});
		} else{
		//账号验证
			conn.open({
				apiUrl: WebIM.config.apiURL,
				user: 'terminal'+$.trim($('#loginact').val()),
				pwd: $.trim($('#loginpsw').val()),
				appKey: WebIM.config.appkey,
				success: function(data) {
					//登录成功
					var user = data.user;
					chatUtils.userInfo.name = user.username;
					$(".loginpage").fadeOut();
				},
				error: function(data) {
					//登录失败
					swal({
					  title: "登录失败",
					  text: "请检查帐号或使用游客登录",
					  timer: 2000,
					  showConfirmButton: false
					});
				
				}
			});
		}
		
	});
	
	$(".actfree").click(function(){
		//免账号按钮
		swal({
			title: "",
			text: "无账号登入？",
			type: 'warning',
			background: '#fff',
			allowOutsideClick: false,
			showCancelButton: true,
			confirmButtonText: "确定",
  			cancelButtonText: "取消",
		}).then(
			//是
			function () {
				loginTourist();
				$(".loginpage").fadeOut();
			},
			//否
			function(dismiss){if (dismiss === 'cancel') {}}
		);
	});	
});